/**
 * @param {number[]} num
 * @param {number} k
 * @return {number[]}
 */
export var addToArrayForm = function (num, k) {
  let [ans, carry] = [[], 0]
  const kArr = k.toString().split('')
  for (let i = 0; i < Math.max(num.length, kArr.length); i++) {
    const sum = (num[num.length - 1 - i] || 0) + (parseInt(kArr[kArr.length - 1 - i]) || 0) + carry
    ans.unshift(sum % 10)
    carry = Math.floor(sum / 10)
  }
  if (carry) ans.unshift(carry)
  return ans
}

/**
 * @param {number[]} num
 * @param {number} k
 * @return {number[]}
 */
export var addToArrayForm2 = function (num, k) {
  let [ans, carry] = [[], 0]
  for (let i = num.length - 1; i >= 0 || k > 0; i--) {
    const sum = (i > -1 ? num[i] : 0) + (k % 10) + carry
    ans.push(sum % 10)
    k = Math.floor(k / 10)
    carry = Math.floor(sum / 10)
  }
  if (carry) ans.push(carry)
  return ans.reverse()
}
